package com.the4man.framework.util;

import org.apache.commons.codec.binary.Base64;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created by lenovo on 2017/12/12.
 */
public class EncryptUtil {
    private static final Logger logger = LoggerFactory.getLogger(EncryptUtil.class);
    private static EncryptUtil epu_ = null;
    public static int _DES = 1;
    public static int _DESede = 2;
    public static int _Blowfish = 3;
    private static String hexKey = "B5584A5D9B61C23BE52CA1168C9110894C4FE9ABC8E9F251";
    private Cipher p_Cipher;
    private SecretKey p_Key;
    private String p_Algorithm;

    public static EncryptUtil getInstance() {
        if(epu_ == null) {
            Class var0 = EncryptUtil.class;
            synchronized(EncryptUtil.class) {
                if(epu_ == null) {
                    epu_ = new EncryptUtil();
                }
            }
        }

        return epu_;
    }

    private EncryptUtil() {
    }

    public String decryptBASE64(String data) {
        try {
            byte[] b = Base64.decodeBase64(data);
            return new String(b, "utf-8");
        } catch (Exception var3) {
            logger.error("BASE64解密失败", var3);
            return null;
        }
    }

    public String encryptBASE64(String data) {
        try {
            byte[] b = Base64.encodeBase64(data.getBytes("utf-8"));
            return new String(b, "utf-8");
        } catch (Exception var3) {
            logger.error("BASE64加密失败", var3);
            return null;
        }
    }

    public String encryptMD5(String str) {
        StringBuffer md5StrBuff = new StringBuffer();
        MessageDigest messageDigest = null;

        try {
            messageDigest = MessageDigest.getInstance("MD5");
            messageDigest.reset();
            messageDigest.update(str.getBytes("UTF-8"));
            byte[] byteArray = messageDigest.digest();

            for(int i = 0; i < byteArray.length; ++i) {
                if(Integer.toHexString(255 & byteArray[i]).length() == 1) {
                    md5StrBuff.append("0").append(Integer.toHexString(255 & byteArray[i]));
                } else {
                    md5StrBuff.append(Integer.toHexString(255 & byteArray[i]));
                }
            }
        } catch (Exception var6) {
            logger.error("MD5加密失败", var6);
        }

        return md5StrBuff.toString();
    }

    private void selectAlgorithm(int al) {
        switch(al) {
            case 1:
            default:
                this.p_Algorithm = "DES";
                break;
            case 2:
                this.p_Algorithm = "DESede";
                break;
            case 3:
                this.p_Algorithm = "Blowfish";
        }

    }

    private SecretKey checkKey() throws NoSuchAlgorithmException {
        if(this.p_Key == null) {
            KeyGenerator keygen = KeyGenerator.getInstance(this.p_Algorithm);
            this.p_Key = keygen.generateKey();
        }

        return this.p_Key;
    }

    private void setKey(byte[] enckey) {
        this.p_Key = new SecretKeySpec(enckey, this.p_Algorithm);
    }

    private byte[] encode(byte[] data) throws Exception {
        this.p_Cipher.init(1, this.checkKey());
        return this.p_Cipher.doFinal(data);
    }

    private byte[] decode(byte[] encdata, byte[] enckey) throws Exception {
        this.setKey(enckey);
        this.p_Cipher.init(2, this.p_Key);
        return this.p_Cipher.doFinal(encdata);
    }

    private String byte2hex(byte[] b) {
        String hs = "";
        String stmp = "";

        for(int i = 0; i < b.length; ++i) {
            stmp = Integer.toHexString(b[i] & 255);
            if(stmp.length() == 1) {
                hs = hs + "0" + stmp;
            } else {
                hs = hs + stmp;
            }
        }

        return hs.toUpperCase();
    }

    private byte[] hex2byte(String hex) throws IllegalArgumentException {
        if(hex.length() % 2 != 0) {
            logger.debug("hex:" + hex + "\nlength:" + hex.length());
            throw new IllegalArgumentException();
        } else {
            char[] arr = hex.toCharArray();
            byte[] b = new byte[hex.length() / 2];
            int i = 0;
            int j = 0;

            for(int l = hex.length(); i < l; ++j) {
                String swap = "" + arr[i++] + arr[i];
                int byteint = Integer.parseInt(swap, 16) & 255;
                b[j] = (new Integer(byteint)).byteValue();
                ++i;
            }

            return b;
        }
    }
}
